package com.happle.gcmclient;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

import com.google.android.gcm.GCMRegistrar;
import com.happle.gcmclient.config.Constants;
import com.happle.gcmclient.config.GlobalValue;
import com.happle.gcmclient.utility.NetworkUtility;

public class MainActivity extends BaseActivity implements View.OnClickListener {

	// This intent filter will be set to filter on the string
	// "GCM_RECEIVED_ACTION"

	private ImageButton btnAsk, btnAllMessage, btnSetting;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initUI();
		if (Constants.SENDER_ID == null) {
			Log.d(TAG, "Missing SENDER_ID");
			return;
		}
		// How to fix this if sender id is missing? To send an error to the
		// server and get update?
		initHeader();
		hideBack();
	}

	@Override
	protected void onResume() {
		super.onResume();
		reloadLanguage();
		if (!NetworkUtility.getInstance(MainActivity.this).isNetworkAvailable()) {
			// Internet Connection is not present
			alertManager.showAlertDialog(MainActivity.this,
					getString(R.string.titleErrorConnection),
					getString(R.string.messageErrorConnection), false);
			// stop executing code by return
			return;
		}
	}

	private void initUI() {
		btnAllMessage = (ImageButton) findViewById(R.id.btnAllConv);
		btnAsk = (ImageButton) findViewById(R.id.btnAsk);
		btnSetting = (ImageButton) findViewById(R.id.btnSettings);
		btnAsk.setOnClickListener(this);
		btnAllMessage.setOnClickListener(this);
		btnSetting.setOnClickListener(this);
		TAG_LOCAL = TAG;
	}

	private void reloadLanguage() {
		//btnAsk.setText(R.string.btn_ask);
		//btnAllMessage.setText(R.string.btn_all_conv);
		//btnSetting.setText(R.string.btn_settings);
		setHeaderTitle(getString(R.string.empty), Constants.TITLE_EMPTY_IMAGE_NUM);
		updateStatusTitle();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	// NOTE the call to GCMRegistrar.onDestroy()
	@Override
	public void onDestroy() {
		GCMRegistrar.onDestroy(this);
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnAsk:
			Intent i = new Intent(self, MessageListActivity.class);
			i.putExtra(Constants.FIELD_REGISTER_ID, GlobalValue.registerId);
			i.putExtra(Constants.FIELD_REQUEST_CODE,
					String.valueOf(Constants.REQUEST_CODE_ADD));
			startActivity(i);
			break;
		case R.id.btnAllConv:
			startActivity(new Intent(self, ConversationActivity.class));
			break;
		case R.id.btnSettings:
			startActivity(new Intent(self, SettingsActivity.class));

			break;

		default:
			break;
		}

	}
}